﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComType;
using ComLib.ComFun;

namespace LocalFileCopying
{
    [XmlRootAttribute("CopyingOption", Namespace = "LocalFileCopying", IsNullable = false)]
    public class CopyingOption : XmlSerializeBase
    {
        /// <summary>
        /// 目标路径
        /// </summary>
        private string _destDirectory;
        [XmlElementAttribute("DestDirectory", IsNullable = true)]
        public string DestDirectory
        {
            get { return _destDirectory; }
            set { _destDirectory = value; }
        }
  
        /// <summary>
        /// 文件复制选项
        /// </summary>
        private CopyOptions _copyOption = CopyOptions.Outcome;
        [XmlElementAttribute("copyOption", IsNullable = false)]
        public CopyOptions CopyOption
        {
            get { return _copyOption; }
            set { _copyOption = value; }
        }

        /// <summary>
        /// 目录格式
        /// </summary>
        private DirectoryFormat _dirFormat = DirectoryFormat.None;
        [XmlElementAttribute("dirFormat", IsNullable = false)]
        public DirectoryFormat DirFormat
        {
            get { return _dirFormat; }
            set { _dirFormat = value; }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            CopyingOption obj = (CopyingOption)Serializer.LoadFromXml(ConfigFilePath, typeof(CopyingOption));
            if (obj != null)
            {
                this._copyOption = obj._copyOption;
                this._destDirectory = obj._destDirectory;
                this._dirFormat = obj._dirFormat;
                this._nVer = obj._nVer;              
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("本地文件复制选项: ");
            Action<string, object> FillString =
                (string field, object val) =>
                {
                    if (val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }                 
                };
            FillString("CopyOption", CopyOption);
            FillString("DestDirectory", DestDirectory);
            FillString("DirectoryFormat", _dirFormat); 
            return builder.ToString();
        }
    }
}
